package es.cazadoresmadridejos.util.itext;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Component;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import es.cazadoresmadridejos.model.BigGameAssistance;

@Component
public class ITextProcessor {
	private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 21, Font.UNDERLINE);
	private Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);

	public void create(OutputStream out,List<BigGameAssistance> list, 
			String cost, String raffleHour, String arrivalHour, String dateString) throws Exception {
		Document document = new Document();
		PdfWriter writer = PdfWriter.getInstance(document, out);
		writer.setPageEvent(new ITextUtil(cost, raffleHour, arrivalHour));
		document.open();
		document.setMargins(10f, 10f, 10f, 10f);
		createDocument(document,list, cost, raffleHour, arrivalHour, dateString);

		document.close(); 
	}

	public void createDocument(Document document,List<BigGameAssistance> list, String cost, String raffleHour, String arrivalHour, String dateString)
			throws DocumentException {
		Paragraph preface = new Paragraph();
		// We add one empty line
		// Lets write a big header
		Paragraph title = new Paragraph("Sociedad de Cazadores de Madridejos", catFont);
		title.setAlignment(Element.ALIGN_CENTER);
		preface.add(title);

		Paragraph bigGame = new Paragraph("MONTERIA", catFont);
		bigGame.setAlignment(Element.ALIGN_CENTER);
		preface.add(bigGame);
		addEmptyLine(preface, 1);

		Paragraph date = new Paragraph(dateString, normal);
		date.setAlignment(Element.ALIGN_CENTER);
		preface.add(date);

		addEmptyLine(preface, 1);

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		java.util.List<String> listString = order(list);

		float fontSize = 12;
		int newLines = 0;
		if(list.size() < 55){
			newLines = 55 - list.size();
			newLines /= 2;
		}else if(list.size() >= 55 && list.size() < 61 ){
			fontSize = 11;
		}else if(list.size() >= 61 && list.size() < 63 ){
			fontSize = 10;
		}else if(list.size() >= 63 && list.size() < 68 ){
			fontSize = 9.5f;
		}else if(list.size() >= 68 && list.size() < 71 ){
			fontSize = 9;
		}else if(list.size() >= 71 && list.size() < 73 ){
			fontSize = 8.5f;
		}else if(list.size() >= 73 && list.size() < 77 ){
			fontSize = 8;
		}else if(list.size() >= 77 && list.size() < 81 ){
			fontSize = 7.5f;
		}else if(list.size() >= 81 && list.size() < 83 ){
			fontSize = 7;
		}else if(list.size() >= 83 && list.size() < 88 ){
			fontSize = 6.5f;
		}else if(list.size() >= 88 && list.size() < 93 ){
			fontSize = 6f;
		}else if(list.size() >= 93 && list.size() < 97 ){
			fontSize = 5.5f;
		}
		for (String string : listString) {
			Font forTable = new Font(Font.FontFamily.TIMES_ROMAN, fontSize, Font.BOLD);
			PdfPCell c1 = new PdfPCell(new Phrase(string,forTable));
			c1.setBorder(Rectangle.NO_BORDER);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);
		}

		preface.add(table);

		addEmptyLine(preface, newLines);
		document.add(preface);
	}

	private java.util.List<String> order(java.util.List<BigGameAssistance> bigGameAssstances){

		java.util.List<BigGameAssistance> r1 = bigGameAssstances.subList(0, bigGameAssstances.size() / 2);
		java.util.List<BigGameAssistance> r2 = bigGameAssstances.subList(bigGameAssstances.size() / 2, bigGameAssstances.size());

		java.util.List<String> result = new ArrayList<String>();

		Iterator<BigGameAssistance> it1 = r1.iterator();
		Iterator<BigGameAssistance> it2 = r2.iterator();

		for (int i = 0; i < r1.size(); i++) {
			BigGameAssistance bga1 = it1.next();
			result.add(bga1.getListIndex() + ". " + bga1.getPartner().getName() + " " + bga1.getPartner().getLastName());

			if(it2.hasNext()){
				BigGameAssistance bga2 = it2.next();
				result.add(bga2.getListIndex() + ". " + bga2.getPartner().getName() + " " + bga2.getPartner().getLastName());
			}
		}

		return result;
	}

	private void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
}
